The pystata magic¶
The %pystata line magic is used to configure the system and display current system information and settings. You can type %pystata? to view its documentation.
[34]:
%pystata?
Docstring:
Stata utility commands.
Line magic syntax:
%pystata status
Display current system information and settings.
%pystata set graph_show True|False [, perm]
Set whether Stata graphics are displayed. The default is to
display the graphics. Note that if multiple graphs are
generated, only the last one is displayed. To display multiple
graphs, use the name() option with Stata's graph commands.
%pystata set graph_size w #[in|px|cm] [, perm]
%pystata set graph_size h #[in|px|cm] [, perm]
%pystata set graph_size w #[in|px|cm] h #[in|px|cm] [, perm]
Set dimensions for Stata graphs. The default is a 5.5-inch width
and 4-inch height. Values may be specified in inches, pixels, or
centimeters; the default unit is inches. Either the width or height
must be specified, or both. If only one is specified, the other one
is determined by the aspect ratio.
%pystata set graph_format svg|png|pdf [, perm]
Set the graphic format used to display Stata graphs. The default
is svg. If svg or png is specified, the graphs will be embedded.
If pdf is specified, the graphs will be displayed, and exported
to PDF files and stored in the current working directory with
numeric names, such as 0.pdf, 1.pdf, 2.pdf, etc. Storing the PDF
graph files in the current directory allows you to embed them in
the notebook when exporting the notebook to a PDF via Latex.
%pystata status
This syntax displays system information and current settings. The system information includes the Python version currently in use, the version and edition of Stata in use, Stata’s shared library path, whether Stata was initialized successfully, and whether the Stata Function Interface (sfi) module was loaded successfully. The system settings include information on whether Stata’s graphs should be displayed, along with their dimensions and display format.
[35]:
%pystata status
System information
Python version 3.7.5
Stata version Stata 17.0 (MP)
Stata library path C:\Program Files\Stata17\mp-64.dll
Stata initialized True
sfi initialized True
Settings
graphic display True
graphic size width = default, height = default
graphic format svg
%pystata set graph_show True|False [, perm]
This syntax specifies whether Stata-generated graphics should be displayed in the cell. The default is True, meaning the graphs are displayed. Once you have specified this setting, it will be in effect for the remainder of the Python session, until it is set again. However, this setting will be forgotten once the current Python session is terminated. If you want this setting to be remembered and become the default setting when you open a new Python session, specify the perm option.
%pystata set graph_size w #[in|px|cm] [, perm]
%pystata set graph_size h #[in|px|cm] [, perm]
%pystata set graph_size w #[in|px|cm] h #[in|px|cm] [, perm]
This syntax is used to set the display dimensions for Stata-generated graphs. You can set one dimension or both. The aspect ratio is respected if only width or only height is set. If units are not specified, in is assumed. Once you have specified this setting, it will be in effect for the remainder of the Python session, until it is set again. However, this setting will be forgotten once the current Python session is terminated. If you want this setting to be remembered and become the default setting when you open a new Python session, specify the perm option.
Below, we set the width of Stata graphs to 4 inches.
[36]:
%pystata set graph_size w 4in
[37]:
%%stata
sysuse auto, clear
scatter mpg price
. sysuse auto, clear
(1978 automobile data)
. scatter mpg price
.
Below, we set the height to 4.5 inches.
[38]:
%pystata set graph_size h 4.5in
[39]:
%stata scatter mpg price
%pystata set graph_format svg|png|pdf [, perm]
This syntax specifies the file format used to display Stata graphs. The default is svg. If svg or png is specified, the graphs are embedded, which means they are exported temporarily to SVG files or PNG files, which are then displayed in the output. If pdf is specified, the graphs are exported to PDF files in the current working directory with numeric names, such as 0.pdf, 1.pdf, 2.pdf, etc. This is useful when you try to export a notebook to a PDF via Latex, in which case the image has to be saved in the same directory as the notebook for the image to be embedded.
[40]:
%pystata set graph_format png
[41]:
%stata scatter mpg price